package com.example.vae.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;

import android.content.Intent;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vae.Dao.PersonData.PersonReposity;
import com.example.vae.Pojo.Person;
import com.example.vae.Pojo.WebPerson;
import com.example.vae.R;
import com.example.vae.Utils.OKHttpUtils;
import com.example.vae.Utils.StatusBarUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.OkHttpClient;


//这个是登陆界面
public class LoginActivity extends AppCompatActivity {

    EditText password, name;
    Button login;
    TextView register;

    private PersonReposity personReposity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_login);
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }


        login = findViewById(R.id.mlogin);
        password = findViewById(R.id.password);
        name = findViewById(R.id.mname);


        personReposity=new PersonReposity(getApplicationContext());

//       在用户登录之前查看是否上次登录的信息
        personReposity.findAllPerson();
        personReposity.setPersonListener(new PersonReposity.PersonListener() {
            @Override
            public void success(List<Person> people) {
                Log.d("这里 ",String.valueOf(people!=null));

                if(people.size()>0){
                    Log.d("vae+ 登陆之前",people.get(0).toString());
                    name.setText(people.get(0).getPersonTelephone());
                    password.setText(people.get(0).getPersonPassword());
                }else {
                    Log.d("vae+ 登陆之前","没有数据");
                }

            }
        });

//        进行登录的时候进行的设置
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = name.getText().toString();
                String spassword = password.getText().toString();
                if (username != null && username != "" && spassword != null && spassword != "") {
                    final Map<String, Object> map = new HashMap<>();
                    map.put("password", spassword);
                    map.put("username", username);
//                    String url = "http://192.168.42.238:8080/ssm05_war/login";
                    String url="http://47.100.104.187:8080/ssm05/login";
                    Log.d("vae", map.toString());
                    OKHttpUtils okHttpUtils = new OKHttpUtils();
                    okHttpUtils.post(url, map);
                    okHttpUtils.setOnOKHttpGetListener(new OKHttpUtils.OKHttpGetListener() {
                        @Override
                        public void error(String error) {
                            Toast.makeText(LoginActivity.this, "对不起请求失败，请检查网络", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void success(String json) {
                            Toast.makeText(LoginActivity.this, json, Toast.LENGTH_SHORT).show();
                            try {
//                                Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
//                                Type type = new TypeToken<Map<String, Object>>() {
//                                }.getType();
//                                Map<String, Object> map1 = gson2.fromJson(json, type);
//                                Log.d("vae", map1.get("users").toString());
//                                Person person = (Person) map1.get("users");

                                Gson gson=new Gson();
                                WebPerson webPerson=gson.fromJson(json,WebPerson.class);

                                Person person=webPerson.getUsers();
                                Log.d("网上查找到的信息 ",person.toString()  );
                                if (webPerson.getCode()==200) {
                                    Log.d("vae+","进行了登录");
//                                   这里先判断里面数据库有没有此用户,先查找此用户,进行更新和插入操作
                                    personReposity.findPersonByID(person);
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    Toast.makeText(LoginActivity.this, "对不起请输入用户信息", Toast.LENGTH_SHORT).show();
                }

            }
        });

//        点击跳转到登录界面
        register = findViewById(R.id.register);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}
